'use strict'

const fs = require('fs')

let options = {
  httpMode : false,
  debug : false,
  logType : 'file',
  test : false,
  port : 1101,
  logDir : './tmp/logs',
  loadType : 'json',
  cert : '',
  key : '',
  server : {},
}

if (process.argv.indexOf('--local-test') > 0) {
  options.httpMode = 'http1'
  options.https = false
  options.debug = true
  options.test = true
  options.port = 1101
  options.cert = ''
  options.key = ''
}

if (process.argv.indexOf('--http-mode') > 0) {
  let ind = process.argv.indexOf('--http-mode')
  
  if (process.argv.length > ind + 1) {
    let hmode = process.argv[ind+1]
    if (['http1', 'http2', 'httpc'].indexOf(hmode) >= 0) {
      options.httpMode = hmode
    }
  }
  
}

if (process.argv.indexOf('--debug') > 0) {
  options.debug = true
}

if (process.argv.indexOf('--log-stdio') > 0) {
  options.logType = 'stdio'
}

if (process.argv.indexOf('--test') > 0) {
  options.test = true
  options.port = 1101
}

if (options.test) {
  options.logDir = '/tmp'
} else {
  try {
    fs.accessSync(options.logDir)
  } catch (err) {
    try {
      fs.mkdirSync(options.logDir)
    } catch (err) {
      console.error(err)
    }
  }

}

if (process.argv.indexOf('--log-dir') > 0) {
  let ind = process.argv.indexOf('--log-dir') + 1
  if (ind < process.argv.length) {
    options.logDir = process.argv[ ind ]
  }
}

if (process.argv.indexOf('--load-text') > 0) {
  options.loadType = 'text'
}

if (process.argv.indexOf('--no-https') > 0) {
  options.cert = ''
  options.key = ''
}

module.exports = options
